//
//  BiCarModifyNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/5/5.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiCarModifyNetEngine.h"


#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiCarModifyParser.h"
#import "ASINetworkQueue.h"
@implementation BiCarModifyNetEngine

SINGLE_INSTANCE_IMPLEMENTION(BiCarModifyNetEngine)

/**
 *   请求变更列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestCarModifyGetList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarCertificateCenter/getList", SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_MODIFY_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_MODIFY_LIST];
}


/**
 *   请求合格证交付,dict为传入参数字典
 */
- (void)requestCarModifySaveCertificate:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarCertificateCenter/saveCertificate", SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_MODIFY_SAVE]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_MODIFY_SAVE];
    
}


/**
 *   请求领条图片上传,dict为传入参数字典
 */
- (void)requestModifyImage:(id)delegate withImge:(UIImage *)image  paraDict:(NSDictionary *)paraDic{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarCertificateCenter/uploadImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_MODIFY_IMAGESAVE]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_CAR_MODIFY_IMAGESAVE];
}

/**
 *   请求领条图片delete,dict为传入参数字典
 */
- (void)requestModifyImageDelete:(id)delegate  paraDict:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarCertificateCenter/deleteImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_MODIFY_IMAGEDEL]];
    
    [self createPostRequest:url paramDic:paraDic tag:NET_GET_CAR_MODIFY_IMAGEDEL];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_GET_CAR_MODIFY_LIST:
        {
            [self onGetModifyListData:request];
        }
            break;
        case NET_GET_CAR_MODIFY_SAVE:
        case NET_GET_CAR_MODIFY_IMAGESAVE:
        case NET_GET_CAR_MODIFY_IMAGEDEL:
        {
            [self onGetModifyCommon:request];
        }
            break;
        default:
            break;
    }
    
}

- (void)onGetModifyListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        id  entity  = [BiCarModifyParser  parserCarModifyListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:entity userInfo:nil];
    }
}


- (void)onGetModifyCommon:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        id  entity  = [BiCarModifyParser  parserCarModifyCommon:[request responseString]];
        [delegate onNetEngineResponse:tag entity:entity userInfo:nil];
    }
}

@end
